package com.sky.financelake.ui.activity;

import android.content.Intent;
import android.os.Bundle;
import android.widget.SearchView;

import androidx.annotation.Nullable;
import androidx.lifecycle.ViewModelProvider;
import androidx.recyclerview.widget.LinearLayoutManager;
import androidx.recyclerview.widget.RecyclerView;

import com.sky.financelake.adapter.StockAdapter;
import com.sky.financelake.base.BaseA;
import com.sky.financelake.databinding.ActivityStockBinding;
import com.sky.financelake.bean.dto.StockDto;
import com.sky.financelake.bean.param.StockParam;
import com.sky.financelake.viewmodels.remote.StockViewModel;

import java.util.ArrayList;
import java.util.List;

import dagger.hilt.android.AndroidEntryPoint;

@AndroidEntryPoint
public class StockA extends BaseA {

    private ActivityStockBinding binding;
    private StockAdapter mStockAdapter;
    private final List<StockDto> stockDtos = new ArrayList<>();
    private StockViewModel mViewModel;

    @Override
    protected void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        binding = ActivityStockBinding.inflate(getLayoutInflater());
        setContentView(binding.getRoot());

        SearchView searchView = binding.searchView;
        RecyclerView recyclerView = binding.recyclerView;

        recyclerView.setLayoutManager(new LinearLayoutManager(this));
        mStockAdapter = new StockAdapter(stockDtos, this);
        mStockAdapter.setOnItemClickListener(new StockAdapter.OnItemClickListener() {
            @Override
            public void onClick(int position) {
                String code = stockDtos.get(position).getSymbol();
                Intent intent = new Intent(StockA.this, KLineA.class);
                intent.putExtra("code", code);
                startActivity(intent);
            }
        });
        recyclerView.setAdapter(mStockAdapter);

        mViewModel = new ViewModelProvider(this).get(StockViewModel.class);
        mViewModel.getStockListLiveData().observe(this, response -> {
            List<StockDto> tmp = response.getResult();
            stockDtos.addAll(tmp);
            mStockAdapter.notifyDataSetChanged();
        });
        loadData();
    }


    public void loadData() {
        StockParam param = new StockParam();
        mViewModel.getStockPagingList(param);
    }
}
